package com.ubao.activity;

import java.io.File;

import com.ubao.activity.ProductDetailActivity.LoadImageThread;
import com.ubao.base.BaseActivity;
import com.ubao.bean.Commodity;
import com.ubao.util.log.Logger;
import com.youxilua.common.fetch.CacheFetch;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

public class ImageActivity extends BaseActivity {
	Commodity commodity;
    Bitmap bitmap;
    ImageView iv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		iv = new ImageView(this);
		iv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		iv.setScaleType(ScaleType.CENTER);
		setContentView(iv);
	}
	
	 public void LoadImage() {
			new LoadImageThread().start();
	}

		class LoadImageThread extends Thread {
			LoadImageThread() {
			}

			public void run() {
				File cacheFile = CacheFetch.dowanLoadBitmap(ImageActivity.this,
						commodity.getImagePath_middle());
				Logger.d("middle:" +cacheFile.getPath());
				try {
					bitmap = BitmapFactory.decodeFile(cacheFile.getPath());
//					drawable = Drawable.createFromPath(cacheFile.getPath());
				} catch (Exception e) {
					e.printStackTrace();
				}
				// if (bitmap != null) {

				// 此处不能直接更新UI，否则会发生异常：
				// CalledFromWrongThreadException: Only the original thread that
				// created a view hierarchy can touch its views.
				// 也可以使用Handler或者Looper发送Message解决这个问题

				ImageActivity.this.runOnUiThread(new Runnable() {
					public void run() {
						if (bitmap != null) {// 此处在线程过多时可能为null
//							int width = bitmap.getWidth();// 获取真实宽高
//							int height = bitmap.getHeight();
	//
//							LayoutParams lp = getLayoutParams();
	//
//							int layoutHeight = (height * getItemWidth()) / width;// 调整高度
//							if (lp == null) {
//								lp = new LayoutParams(getItemWidth(), layoutHeight);
//							}
//							setLayoutParams(lp);

//							setImageBitmap(bitmap);
//							setImageDrawable(drawable);
//							Handler h = getViewHandler();
//							Message m = h.obtainMessage(Constants.HANDLER_WHAT,
//									width, layoutHeight, FlowView.this);
//							h.sendMessage(m);
						}
					}

//					private void setImageBitmap(Bitmap bitmap) {
//						// TODO Auto-generated method stub
//						ivProductImg.setImageBitmap(bitmap);
//					}
//					
//					private void setImageDrawable(Drawable drawable) {
//						// TODO Auto-generated method stub
//						ivProductImg.setBackgroundDrawable(drawable);
//					}
					
				});
			}
		}

		@Override
		protected void initView() {
			// TODO Auto-generated method stub
			
		}

		@Override
		protected void getIntentData() {
			// TODO Auto-generated method stub
			
		}

		@Override
		protected void setIntentData() {
			// TODO Auto-generated method stub
			
		}

}
